//Sample Descripter By Allen Wu //Extra classes requirements: wslib Quark. //SampleAnalyse is using SCMIR. Make sure you have SCMIR installed in your SuperCollider extensions. http://composerprogrammer.com/code.html SampleDescript{ classvar guiTempFile; classvar <> pitchThresh; //raw data var keynum; var =0 && "0123456789".includes(str.at(i))},{i=i-1}); if (i= 2){// had more than 2 digits at end if(l - i <=3){//had no more than 3 digits, make it key number; keynumFromFileName = str.copyRange(i+1,l).asInteger; } {// if there are more than 3 digits at the end of file //that is probably not a key number. keynumFromFileName = nil} } {//had only one digits at end j=i; while ({i>=0 && "AaBbCcDdEeFfGg#Ss".includes(str.at(i))},{i=i-1}); if (i= pitchThresh) { pitchCollection = pitchCollection.add(thisPitch) }; }; //if no picth data is collected, find pitch before peak time if(pitchCollection.size == 0) { if(peakTime[sectionIndex]-startTime[sectionIndex] <= (hoptime * 20)) { var pitchArray = pitchData[startIndex[sectionIndex]..thisPeakIndex[sectionIndex]].flop; pitch = pitchArray[0]; hasPitch = pitchArray[1]; } { var pitchArray = pitchData[(thisPeakIndex - 20)..thisPeakIndex].flop; pitch = pitchArray[0]; hasPitch = pitchArray[1]; }; pitch.do{|thisPitch, index| if(hasPitch[index] >= pitchThresh) { pitchCollection = pitchCollection.add(thisPitch) }; }; }; //if no pitch data is collected, than use centroid data for pitch //if there are pitch data collected, get the most occurred data for keynum if((pitchCollection.size == 0) || (pitchCollection.occurrencesArray(0.5).maxItem == 1)) {keynumFromPitchFound = keynumFromPitchFound.add((centroidData[thisPeakIndex] ? centroidData[centroidData.size-1]).explin(20, 20000, 28, 103) - 12); // an octave lower to map to the range of my keyboard :p "no pitch detected, using centorid".postln;} {keynumFromPitchFound = keynumFromPitchFound.add(pitchCollection.mostOccurredItems(0.5).mean)}; }); //Now determine which answer to use if(filenameAsNote && keynumFromFileName.isNil.not) {keynum = Array.fill(peakIndex.size, keynumFromFileName) + pitchShift;} {keynum = keynumFromPitchFound + pitchShift;} }//end of getKeyNum //******************************************************************************** //Separate sound file into recognizable sound sections if exist. //Here is how it works: //1. Find out onsets in this sound file using SCMIR onset data. //2. Find section breaking point based on the RMS trough point inbetween each onsets. //3. Find peak point inbetween each sections. //4. Find attact and ending point from the peak point using fixed threshold method. //get onset time, if an onset is too close to previous one, it will be abandoned getOnsetTime {|groupingThresh = 0.32| var onsets = [file.onsetdata[0]]; file.onsetdata.doAdjacentPairs({|thisOnset, nextOnset| //filter onsets too close if((nextOnset - thisOnset) > groupingThresh, {onsets = onsets.add(nextOnset)}); }); if(onsets[0].isNil){onsets[0] = 0}; onsetTime = onsets; } //get frame index at the onset time getOnsetIndex { onsetIndex = (onsetTime / hoptime).asInteger; } //Find breakpoint of a sample file by onsets. //using lowest point in between onsets to be section breakpoints. findBreakPointByOnsets { var troughArray = []; sectionBreakPoint = []; onsetIndex.do{|thisOnset, index| var previousOnset = onsetIndex[index - 1]; troughArray = troughArray.add(rmsData[previousOnset..thisOnset].lastMinIndex + (previousOnset ?? 0)); }; sectionBreakPoint = troughArray; } //Separate rmsData into subsections by breakpoints. sectionRmsDataByOnset { var output = []; rmsDataBySection = []; sectionBreakPoint.do{|thisSection, index| var nextSection = sectionBreakPoint[index + 1]; output = output.add(rmsData[thisSection..nextSection]); }; rmsDataBySection = output; } //find local peaks in the section breakpoint findPeaksByOnsets { var peakArray = [], peakAmpArray = []; var soundFile = SoundFile.openRead(filename); var soundFileArray = FloatArray.newClear(soundFile.numFrames*soundFile.numChannels); peakIndex = []; peakTime = []; peakAmp = []; soundFile.readData(soundFileArray); soundFileArray = soundFileArray.clump(soundFile.numChannels).flop; //[[channel 1], [channel 2],....] sectionBreakPoint.do{|thisSection, index| var nextSection = sectionBreakPoint[index + 1]; var peakhop = rmsData[thisSection..nextSection].maxIndex + thisSection; //find the biggest rms session var thisPeakLevel; var peaksInTheHop = []; soundFileArray.numChannels.do{|channel| var channelPeakPoint = (soundFileArray[channel].abs[peakhop*SS_SCMIR.framehop..(peakhop+1)*SS_SCMIR.framehop].maxIndex)/SS_SCMIR.framehop; var channelPeakLevel = soundFileArray[channel].abs[peakhop*SS_SCMIR.framehop..(peakhop+1)*SS_SCMIR.framehop].maxItem; peaksInTheHop = peaksInTheHop.add([channelPeakPoint, channelPeakLevel]); }; peaksInTheHop = peaksInTheHop.flop; //[peakPoints, peakLevels] peakAmpArray = peakAmpArray.add(peaksInTheHop[1].maxItem); peaksInTheHop = peaksInTheHop[0][peaksInTheHop[1].maxIndex]; peakArray = peakArray.add(peakhop + peaksInTheHop); }; globalPeakAmp = peakAmpArray.maxItem; peakIndex = peakArray.select({|item, i| peakAmpArray[i] > (globalPeakAmp * 0.1)}); sectionBreakPoint = sectionBreakPoint.select({|item, i| peakAmpArray[i] > (globalPeakAmp * 0.1)}); peakAmp = peakAmpArray.select({|item, i| item > (globalPeakAmp * 0.1)}); globalPeakIndex = peakIndex[peakAmpArray.maxIndex]; globalPeakTime = peakTime[peakAmpArray.maxIndex]; peakTime = peakIndex * hoptime; } //Dictate attact/release time arEnv {|startThresh, endThresh| var startAmp; var endAmp; var thisSectionGlobalIndex = 0; startIndex = []; endIndex = []; startTime = []; endTime = []; attackDur = []; releaseDur = []; activeDuration = []; //for each onset section, find peaks rmsDataBySection.do{|thisSection, sectionIndex| var thisSectionStartIndex = sectionBreakPoint[sectionIndex]; var startAmp = startThresh * peakAmp[sectionIndex]; var endAmp = endThresh * peakAmp[sectionIndex]; //startTime: //search for the first point pass above threshold. //MAYBE: Use adaptative threshold method (weakest effort method) for start time detection. //Peeters, Geoffroy. “A Large Set of Audio Features for Sound Description (Similarity and Description) in the Cuidado Project.” IRCAM, Paris, France (2004). block{|break| thisSection.do({|rmsenergy, index| if(rmsenergy >= startAmp) { startIndex = startIndex.add(thisSectionStartIndex + index); startTime = startTime.add(startIndex.last * hoptime); break.value(0); }; }) }; //search for the last point pass below threshold. block{|break| var reversePeak = thisSection.reverse.maxIndex; thisSection.reverseDo({|rmsenergy, index| if(rmsenergy >= endAmp) { endIndex = endIndex.add(sectionBreakPoint[sectionIndex] + thisSection.size - index); endTime = endTime.add(endIndex.last * hoptime); break.value(0); }; }) }; }; attackDur = peakTime - startTime; releaseDur = endTime - peakTime; activeDuration = endTime - startTime; } //Separate Datas into subsections by active envelopes. //includes: //activeRMSData //activeCentroid //activeDissonanace //temporalCentorid getActiveData { //Datas to retrive activeCentroidData = []; activeNoiseData = []; temporalCentroid = []; activeRMSData = []; startIndex.do({|thisStartIndex, sectionIndex| var thisRMSData = rmsData[thisStartIndex..endIndex[sectionIndex]]; var activeTime = frameTimes[thisStartIndex..endIndex[sectionIndex]] - frameTimes[thisStartIndex]; activeRMSData=activeRMSData.add(thisRMSData); activeCentroidData = activeCentroidData.add(centroidData[thisStartIndex..endIndex[sectionIndex]]); activeNoiseData = activeNoiseData.add(noiseData[thisStartIndex..endIndex[sectionIndex]]); temporalCentroid = temporalCentroid.add( (thisRMSData*activeTime).sum / thisRMSData.sum; ) }) } //MFCC of a sound is decided arbitrarily to be the average MFCC data of 20 frames around the peak point getMFCC { var start, end; mfcc = []; peakIndex.do({|thisPeakIndex, sectionIndex| thisPeakIndex = thisPeakIndex.asInteger; if(thisPeakIndex-startIndex[sectionIndex] <= 10){start = startIndex[sectionIndex]}{start = thisPeakIndex - 10}; if(endIndex[sectionIndex]-thisPeakIndex <= 10){end = endIndex[sectionIndex]}{end = thisPeakIndex - 10}; mfcc = mfcc.add(mfccData[start..end].flop.collect({|data| data.sum/data.size})); }); ^mfcc; } //play the sound file, using(at) to play each onset. If (at) is larger than the last onset index, it plays a random onset. play {arg at = nil, out = 0, server, detune = 0, pan = 0, level = 1; var buf, rate, cond = Condition.new(false); server = server ? Server.default; rate = 2 ** (detune / 12); if(buffer == []) {server.waitForBoot{this.loadToBuffer(server, action: {cond.test = true; cond.signal;})}} {cond.test = true; cond.signal;}; Routine.run{ cond.wait; if(at==nil) {buf = buffer} {//if the index number is not in the range of section numbers, play a random section if(at.asInteger >= 0 && at.asInteger < activeBuffer.size) {buf = activeBuffer[at.asInteger]} {buf = activeBuffer[activeBuffer.size.rand]} }; if(buf.size == 1) {{Pan2.ar(PlayBuf.ar(1, buf[0], doneAction: 2, rate: BufRateScale.ir(buf[0]) * rate), pan, level)}.play(outbus: out)} {{Balance2.ar(PlayBuf.ar(1, buf[0], doneAction: 2, rate: BufRateScale.ir(buf[0]) * rate), PlayBuf.ar(1, buf[1], doneAction: 2, rate: BufRateScale.kr(buf[1]) * rate), pan, level)}.play(outbus: out)}; } } //return an envelope to represent the whole sound file env { var frametimes; frametimes = Array.series(rmsData.size, 0, SS_SCMIR.framehop / sampleRate); ^Env.pairs([frametimes, rmsData].flop, \lin).duration_(duration); // Why is duration adjustment necessary? //^Env.pairs([frameTimes, rmsData].flop, \lin); } //return an array of envelopes to represent each onsets. getActiveEnv {|startThresh=0.01, endThresh=0.01| var envArray = []; this.arEnv(startThresh, endThresh); rmsDataBySection.do{|thisSection, sectionIndex| var activeFrameTimes; var activeRmsData; activeFrameTimes = frameTimes[startIndex[sectionIndex]..endIndex[sectionIndex]] - frameTimes[startIndex[sectionIndex]]; activeRmsData = rmsData[startIndex[sectionIndex]..endIndex[sectionIndex]]; envArray = envArray.add(Env.pairs([activeFrameTimes, activeRmsData].flop, \lin)); }; activeEnv = envArray; } plot { this.buffer.plot; } }